import { getAddress, setAddress } from '@/utils/storage'
import request from '@/utils/request'
import store from '@/store'
export default {
  namespaced: true,
  state () {
    return {
      addresses: getAddress() || {}
    }
  },
  mutations: {
    setAddressList (state, { newAddressList, address, information }) {
      state.addresses.newAddressList = newAddressList
      state.addresses.address = address
      state.addresses.information = information
      setAddress({ newAddressList, address, information })
    }
  },
  actions: {
    async getAddressAction (context) {
      const userId = store.state.user.userInfo.userId
      const res = await request.get('/getAddressList',
        {
          params: { userId },
          headers: {
            Authorization: store.state.user.userInfo.token
          }
        })
      if (res.status === 200) {
        const address1 = res.data.filter((item) => item.mode === '默认')
        const address = address1[0]
        const newAddressList = res.data
        const { user, phone } = address
        const information = { user, phone }
        context.commit('setAddressList', { newAddressList, address, information })
      } else if (res.status === 404) {
        context.commit('setAddressList', { newAddressList: [], address: {}, information: {} })
      }
    },
    async addAddressAction (context, { mode, user, phone, local, detailLocal }) {
      const userId = store.state.user.userInfo.userId
      const res = await request.post('/addAddress', {
        mode,
        user,
        phone,
        local,
        detailLocal,
        userId
      }, {
        headers: {
          Authorization: store.state.user.userInfo.token
        }
      })

      if (res.status === 200) {
        context.dispatch('getAddressAction')
      } else if (res.status === 400) {
        alert(res.message)
      }
    },
    async updateAddressAction (context, { index, mode, user, local, detailLocal }) {
      const userId = store.state.user.userInfo.userId
      const phone = context.state.addresses.newAddressList[index].phone
      const res = await request.post('/updateAddress', {
        userId,
        phone,
        mode,
        user,
        local,
        detailLocal
      }, {
        headers: {
          Authorization: store.state.user.userInfo.token
        }
      })
      if (res.status === 200) {
        context.dispatch('getAddressAction')
      } else if (res.status === 400) {
        alert(res.message)
      }
    },
    async setDefaultAddressAction (context, index) {
      const userId = store.state.user.userInfo.userId
      const { phone } = context.state.addresses.newAddressList[index]
      await request.post('/changeDefaultAddress', { userId, phone }, {
        headers: {
          Authorization: store.state.user.userInfo.token
        }
      })
      context.dispatch('getAddressAction')
    },
    async deleteAddressAction (context, index) {
      console.log(index)
      const userId = store.state.user.userInfo.userId
      const { phone } = context.state.addresses.newAddressList[index]
      const res = await request.post('/deleteAddress', { userId, phone }, {
        headers: {
          Authorization: store.state.user.userInfo.token
        }
      })
      if (res.status === 200) {
        context.dispatch('getAddressAction')
      } else if (res.status === 400) {
        alert(res.message)
      }
    }
  },
  getters: {}
}
